package com.jzz.eletuanbackend.config;

import com.jzz.eletuanbackend.interceptors.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private BaseTokenInterceptor baseTokenInterceptor;

    @Autowired
    private LoginInterceptor loginInterceptor;

    @Autowired
    private CustomerInterceptor customerInterceptor;

    @Autowired
    private DelivererInterceptor delivererInterceptor;

    @Autowired
    private BusinessInterceptor businessInterceptor;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX + "/templates/")
                .addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX + "/static/");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry
                .addInterceptor(baseTokenInterceptor)
                .addPathPatterns("/**");

        registry
                .addInterceptor(customerInterceptor)
                .addPathPatterns("/customer/order/**", "/customer/receiveInfo/edit");

        registry
                .addInterceptor(delivererInterceptor)
                .addPathPatterns("/deliverer/**")
                .excludePathPatterns("/deliverer/position/get");

        registry
                .addInterceptor(businessInterceptor)
                .addPathPatterns("/business/**");

    }

}
